<template>
  <div id="home_page">
    <!-- 搜索框 -->
    <div class="search_bar">
      <a-input-search
        v-model:value="searchParams.searchText"
        placeholder="从海量图中搜索"
        size="large"
        enter-button="搜索"
        @search="doSearch"
      />
    </div>
    <!-- 分类和选项标签 -->
    <a-tabs v-model:activeKey="selectedCategory" @change="changeTab">
      <a-tab-pane  key="all" tab="全部" />
      <a-tab-pane  v-for="category in categoryList" :key="category" :tab="category" />
    </a-tabs>
    <div class="tag_bar">
      <span style="margin-right: 8px">标签:</span>
      <a-space :size="[0, 8]" wrap>
        <a-checkable-tag
          v-for="(tag, index) in tagList"
          :key="tag"
          v-model:checked="selectedTagList[index]"
          @change="doSearch"
        >
          {{ tag }}
        </a-checkable-tag>
      </a-space>
    </div>
    <!-- 图片列表 -->
    <picture-list :dataList="dataList" :loading="loading" />
    <!-- 分页器 -->
    <a-pagination
      style="text-align: right"
      v-model:current="searchParams.pageNum"
      v-model:pageSize="searchParams.pageSize"
      :total="total"
      @change="onPageChange"
    />
  </div>
</template>
<script lang="ts" setup>
import {
  listPictureTagCategoryUsingGet,
  listPictureVoByPageUsingPost,
} from '@/request/api/pictureController'
import { message } from 'ant-design-vue'
import { ref, onMounted, computed, reactive } from 'vue'
import { useRouter } from 'vue-router'
import PictureList from '@/components/PictureList.vue'

const dataList = ref<API.PictureVO[]>([])
const loading = ref<boolean>(false)
const total = ref<number>(0)
const searchParams = reactive<API.PictureQueryRequest>({
  pageNum: 1,
  pageSize: 12,
  sortFields: ['createTime'],
  sortOrders: [0], // 0升序 1降序
})
// 标签和分类列表
const categoryList = ref<string[]>([])
// 对应的key高亮显示
const selectedCategory = ref<string>('all')
const tagList = ref<string[]>([])
// 值为true在tagList中高亮显示
const selectedTagList = ref<boolean[]>([])

// 获取图片列表
const featureData = async () => {
  loading.value = true
  // 转换搜索参数
  const params = {
    ...searchParams,
    tags: [] as string[],
  }
  if (selectedCategory.value != 'all') {
    // 表示携带带分类条件
    params.category = selectedCategory.value
  }

  /*
  tagList:['java','java1','java2']
  selectedTagList:[true,false,false]=>['java']
  */
  selectedTagList.value.forEach((useTag, index) => {
    // useTag为true的话那对应在tagList中被选中了,那么就添加到搜索的tags列表里
    if (useTag) {
      params.tags.push(tagList.value[index])
    }
  })
  const { data: res } = await listPictureVoByPageUsingPost(params)
  if (res.code === 0 && res.data) {
    dataList.value = res.data.records ?? []
    total.value = res.data.total ?? 0
  } else {
    message.error(res.message ?? '获取图片列表失败')
  }
  loading.value = false
}
// 搜索
const doSearch = () => {
  // 重置到第一页
  searchParams.pageNum = 1
  // 携带条件去搜索
  featureData()
}
// 切换tab分类时触发
const changeTab=()=>{
  featureData()
}

// 获取标签和分类选项
const getCategoryListTagList = async () => {
  const { data: res } = await listPictureTagCategoryUsingGet()
  if (res.code === 0 && res.data) {
    categoryList.value = res.data.categoryList ?? []
    tagList.value = res.data.tagList ?? []
  } else {
    message.error('获取标签分类列表失败' + res.message)
  }
}
// 分页器切换时触发
const onPageChange = (page: number, pageSize: number) => {
  // 分页器发生改变时
  searchParams.pageNum = page
  searchParams.pageSize = pageSize
  featureData()
}
// 点击图片
const router = useRouter()
const doClickPicture = (picture: API.PictureVO) => {
  router.push({
    path: `/picture/${picture.id}`,
  })
}

onMounted(() => {
  featureData()
  getCategoryListTagList()
})
</script>
<style lang="less" scoped>
#home_page {
  margin-bottom: 16px;
  .search_bar {
    max-width: 480px;
    margin: 0 auto 16px;
  }
  .tag_bar {
    margin-bottom: 16px;
  }
}
</style>
